package operations;

import MyThrowable.IDRepeatWrongful;
import student.*;


import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2022-12-14
 * Time: 19:57
 */
public class SortOperation implements IOperation{
    static String PatternStr2 = "^[0-9]+$";

    @Override
    public void work(StudentList studentList) throws IDRepeatWrongful {
        System.out.println("请选择排序的类型");
        while (true){
            int choice = menu();
            int flag = sortoperation(choice,studentList);
            if(flag == 0){
                break;
            }
        }

    }

    private int sortoperation(int choice, StudentList studentList) {
        switch (choice){
            case 1:
                System.out.println("按学号排");
                studentList.SortCompare(new idCompare());
                return 1;
            case 2:
                System.out.println("按姓名排");
                studentList.SortCompare(new nameCompare());
                return 1;
            case 3:
                System.out.println("按专业排");
                studentList.SortCompare(new msCompare());
                return 1;
            case 4:
                System.out.println("按年龄排");
                studentList.SortCompare(new ageCompare());
                return 1;
            case 5:
                System.out.println("按性别排");
                studentList.SortCompare(new genderCompare());
                return 1;
            case 6:
                return 0;
            default:
                System.out.println("请重新输入!");
                return 1;

        }

    }

    private int menu(){
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("*******************************");
            System.out.println("*******请选择排序的类型(1~6)******");
            System.out.println("********** 1.学号 **************");
            System.out.println("********** 2.姓名 **************");
            System.out.println("********** 3.专业 **************");
            System.out.println("********** 4.年龄 **************");
            System.out.println("********** 5.性别 **************");
            System.out.println("******** 6.返回上一级 ***********");
            System.out.println("*******************************");
            String choice = scan.nextLine();
            if (Pattern.matches(PatternStr2, choice)) {
                return Integer.valueOf(choice);
            }else {
                System.out.println("输入有误!");
            }
        }


    }


}
